package com.example.finalassignment.interceptor;

import com.example.finalassignment.common.ThreadContext;
import com.example.finalassignment.util.JwtUtil;
import jakarta.servlet.*;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Enumeration;

/**
 * 最顶级的过滤器
 */

@Component
public class BoosFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        ThreadContext.setThreadLocalValue();

        HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
        String token = httpServletRequest.getHeader("token");
        if(token != null){
            ThreadContext.setKeyAndValue("token",token);
            String id = JwtUtil.getIdByToken(ThreadContext.getToken());
            ThreadContext.setKeyAndValue("userId",id);
        }

        filterChain.doFilter(servletRequest, servletResponse);
    }
}
